import urllib.parse
import urllib.request

import json
import uuid

import datetime
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

from utils import Utils

pre_url = "https://oapi.dingtalk.com/robot/send?access_token=b6853949d0651e987a1b9eb162c4a4716fd000234e593478af94103c5866f0c7"


class DingHelper(object):

    def ding_kucun(self, content):
        header = {
            "Content-Type": "application/json",
            "Charset": "UTF-8"
        }
        # 构建请求数据，post请求
        nowTime = datetime.datetime.now().strftime('%H:%M:%S')
        data = {
            "msgtype": "markdown",
            "markdown": {
                 "title": "库存变化数据",
                 "text": "#### {}：\n><font color=#0000FF size=2 >{}</font>".format(nowTime, content)
             },
            "at": {
                "isAtAll": True  # @全体成员（在此可设置@特定某人）
            }
        }
        # 对请求的数据进行json封装
        sendData = json.dumps(data)
        sendData = sendData.encode("utf-8")

        # 发送请求
        url = pre_url + Utils.ding_sign()
        request = urllib.request.Request(url=url, data=sendData, headers=header)
        # 将请求发回的数据构建成为文件格式
        opener = urllib.request.urlopen(request)